package org.groupg.practice.guiPractice.tools;

import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;

public class AdvancedSwingExample {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame frame = new JFrame("高级 Swing 组件");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);

            JTabbedPane tabbedPane = new JTabbedPane();
            tabbedPane.addTab("表格", createTablePanel());
            tabbedPane.addTab("树", createTreePanel());
            tabbedPane.addTab("菜单", createMenuPanel());
            tabbedPane.addTab("对话框", createDialogPanel());

            frame.add(tabbedPane);
            frame.setVisible(true);
        });
    }

    private static JPanel createTablePanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 创建表格数据
        String[] columnNames = {"姓名", "年龄", "城市"};
        Object[][] data = {
            {"张三", 25, "北京"},
            {"李四", 30, "上海"},
            {"王五", 28, "广州"},
            {"赵六", 35, "深圳"}
        };

        DefaultTableModel model = new DefaultTableModel(data, columnNames) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return column != 0; // 只有姓名列不可编辑
            }
        };

        JTable table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);

        panel.add(scrollPane, BorderLayout.CENTER);

        // 添加按钮用于操作表格
        JPanel buttonPanel = new JPanel();
        JButton addButton = new JButton("添加行");
        JButton deleteButton = new JButton("删除选中行");

        addButton.addActionListener(e -> {
            model.addRow(new Object[]{"新人员", 0, "未知城市"});
        });

        deleteButton.addActionListener(e -> {
            int selectedRow = table.getSelectedRow();
            if (selectedRow != -1) {
                model.removeRow(selectedRow);
            }
        });

        buttonPanel.add(addButton);
        buttonPanel.add(deleteButton);
        panel.add(buttonPanel, BorderLayout.SOUTH);

        return panel;
    }

    private static JPanel createTreePanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 创建树节点
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("根节点");

        DefaultMutableTreeNode category1 = new DefaultMutableTreeNode("分类1");
        category1.add(new DefaultMutableTreeNode("子节点1.1"));
        category1.add(new DefaultMutableTreeNode("子节点1.2"));

        DefaultMutableTreeNode category2 = new DefaultMutableTreeNode("分类2");
        category2.add(new DefaultMutableTreeNode("子节点2.1"));
        category2.add(new DefaultMutableTreeNode("子节点2.2"));

        root.add(category1);
        root.add(category2);

        JTree tree = new JTree(root);
        tree.addTreeSelectionListener(e -> {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                tree.getLastSelectedPathComponent();
            if (node != null) {
                System.out.println("选中节点: " + node.getUserObject());
            }
        });

        JScrollPane scrollPane = new JScrollPane(tree);
        panel.add(scrollPane, BorderLayout.CENTER);

        return panel;
    }

    private static JPanel createMenuPanel() {
        JPanel panel = new JPanel(new BorderLayout());

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();

        // 文件菜单
        JMenu fileMenu = new JMenu("文件");
        JMenuItem newItem = new JMenuItem("新建");
        JMenuItem openItem = new JMenuItem("打开");
        JMenuItem saveItem = new JMenuItem("保存");
        JMenuItem exitItem = new JMenuItem("退出");

        exitItem.addActionListener(e -> System.exit(0));

        fileMenu.add(newItem);
        fileMenu.add(openItem);
        fileMenu.add(saveItem);
        fileMenu.addSeparator();
        fileMenu.add(exitItem);

        // 编辑菜单
        JMenu editMenu = new JMenu("编辑");
        JMenuItem cutItem = new JMenuItem("剪切");
        JMenuItem copyItem = new JMenuItem("复制");
        JMenuItem pasteItem = new JMenuItem("粘贴");

        editMenu.add(cutItem);
        editMenu.add(copyItem);
        editMenu.add(pasteItem);

        // 帮助菜单
        JMenu helpMenu = new JMenu("帮助");
        JMenuItem aboutItem = new JMenuItem("关于");

        aboutItem.addActionListener(e ->
            JOptionPane.showMessageDialog(panel, "Swing 示例程序\n版本 1.0"));

        helpMenu.add(aboutItem);

        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);

        // 创建工具栏
        JToolBar toolBar = new JToolBar();
        toolBar.add(new JButton(new AbstractAction("新建") {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(panel, "新建操作");
            }
        }));
        toolBar.addSeparator();
        toolBar.add(new JButton(new AbstractAction("保存") {
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(panel, "保存操作");
            }
        }));

        panel.add(toolBar, BorderLayout.NORTH);
        panel.add(new JLabel("菜单和工具栏示例", SwingConstants.CENTER), BorderLayout.CENTER);

        // 注意：在实际应用中，菜单栏应该添加到JFrame上
        // frame.setJMenuBar(menuBar);

        return panel;
    }

    private static JPanel createDialogPanel() {
        JPanel panel = new JPanel(new GridLayout(2, 2, 10, 10));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        JButton messageDialog = new JButton("消息对话框");
        JButton confirmDialog = new JButton("确认对话框");
        JButton inputDialog = new JButton("输入对话框");
        JButton fileDialog = new JButton("文件对话框");

        messageDialog.addActionListener(e ->
            JOptionPane.showMessageDialog(panel, "这是一个消息对话框", "消息",
                JOptionPane.INFORMATION_MESSAGE));

        confirmDialog.addActionListener(e -> {
            int result = JOptionPane.showConfirmDialog(panel, "你确定要执行此操作吗?",
                "确认", JOptionPane.YES_NO_OPTION);
            if (result == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(panel, "你选择了是");
            }
        });

        inputDialog.addActionListener(e -> {
            String input = JOptionPane.showInputDialog(panel, "请输入你的名字:", "输入",
                JOptionPane.QUESTION_MESSAGE);
            if (input != null && !input.trim().isEmpty()) {
                JOptionPane.showMessageDialog(panel, "你好, " + input + "!");
            }
        });

        fileDialog.addActionListener(e -> {
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showOpenDialog(panel);
            if (result == JFileChooser.APPROVE_OPTION) {
                JOptionPane.showMessageDialog(panel,
                    "选择的文件: " + fileChooser.getSelectedFile().getName());
            }
        });

        panel.add(messageDialog);
        panel.add(confirmDialog);
        panel.add(inputDialog);
        panel.add(fileDialog);

        return panel;
    }
}
